package aimsite

import (
	"banzhuan-sys/model"
	"banzhuan-sys/pkg/db"
	"banzhuan-sys/pkg/utils"
	"fmt"
	"github.com/gocolly/colly"
	"github.com/tidwall/gjson"
	"gorm.io/gorm"
	"math"
	"strconv"
	"time"
)

const DuiYou_API = "https://api.duiyou360.com/"

type DuiYou struct {
	Title string
	Url string
}

func (dy *DuiYou) Run() (bool,error) {


	totalPage := 0
	nowPage := 1
	//
	post := model.Posts{}
	var posts []model.Posts
	var all []model.Posts
	totalCount := 0
	var count int64

	db.My.Model(&model.Posts{}).Where("cat_id = ?", YDY_ID).Count(&count)

	c := colly.NewCollector(
		colly.UserAgent(utils.RandomUA()),
		//colly.MaxDepth(1),
	)

	c.Limit(&colly.LimitRule{
		DomainGlob: "*",
		Delay:      5 * time.Second,
	})

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL)
	})

	c.OnResponse(func(r *colly.Response) {

		json := string(r.Body)
		page,_ := strconv.Atoi(r.Request.URL.Query().Get("pageNum"))
		if page == 1 {
			totalCount,_ = strconv.Atoi(gjson.Get(json, "pageinfo.totalCount").String())
			totalPage = int(math.Ceil(float64(totalCount)/float64(15)))
		}

		result := gjson.Get(json, "data")
		posts = make([]model.Posts, 0)
		result.ForEach(func(key, value gjson.Result) bool {

			post.Title = value.Get("title").String()
			post.Href = YDY+"/job/detail/"+value.Get("jobId").String()
			post.CatId = YDY_ID

			if count == 0 {
				posts = append(posts, post)
			}else{
				res := db.My.Where("href = ?", post.Href).First(&model.Posts{})
				if res.Error == gorm.ErrRecordNotFound {
					posts = append(posts, post)
				}
			}
			return true
		})

		all = append(all, posts...)

		if len(all) == 0 {
			return
		}

		if nowPage == totalPage {

			for i, j := 0, len(all)-1; i < j; i, j = i+1, j-1 {
				all[i], all[j] = all[j], all[i]
			}
			db.My.Create(&all)
			return
		}
		nowPage++

		c.Visit(DuiYou_API + "/job/getHotJobList?limit=15&pageNum="+strconv.Itoa(nowPage))

	})

	c.OnScraped(func(r *colly.Response) {
		fmt.Println("结束", r.Request.URL)
	})


	c.Visit(DuiYou_API + "/job/getHotJobList?limit=15&pageNum=1")

	return true, nil

}